#include "Stack.h"

void STInit(ST* ps) 
{
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}



void SLPushback(ST* ps,STDatatype x)
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		STDatatype newcapacity = ps->capacity == 0 ? sizeof(STDatatype) : 2 * ps->capacity;
		ST* ret = (STDatatype*)malloc(sizeof(STDatatype) * newcapacity);
		if (ret == NULL)
		{
			perror("malloc");
			return ;
		}
		ps->arr = ret;
		ps->capacity = newcapacity;
	}
	ps->arr[ps->top] = x;
	ps->top++;
}

void STDestroy(ST* ps)
{
	ps->arr = NULL;
	ps->capacity = ps->top = 0;
}

int STEmpty(ST* ps)
{
	return ps->top == 0;
}
void Stackpop(ST* ps)
{
	assert(!STEmpty(ps));
	ps->top--;
}

STDatatype StackTop(ST* ps)
{
	assert(!STEmpty(ps));
	return ps->arr[ps->top - 1];
}

int STNum(ST* ps)
{
	assert(ps);
	return ps->top ;
}